<script setup>
import {ref, computed,} from 'vue'

const props = defineProps({
  score: { // 公司z值记分
    type: Number,
    default: 0,
    required: true
  }
})

const badScore = 1.88; // Z值小于1.81，‌则表明公司存在很大的破产危险
const goodScore = 2.99; // 财务状况良好

const useRate = () => {

  const current = computed(() => {
    // 风险等级
    const score = props.score;
    if (score >= goodScore) {
      return 2;
    } else if (score >= badScore) {
      return 1;
    } else {
      return 0;
    }
  })

  const leftStyle = computed(() => {
    const val = props.score;
    let pct = 0;
    if (val >= goodScore) {
      pct = val === goodScore ? 1 : 30;
    } else if (val > badScore) {
      pct = (val / goodScore) * 100;
    } else {
      pct = 45;
    }
    return {
      left: pct + '%'
    }
  })
  return {
    current, leftStyle,
  }

}
const {current, leftStyle,} = useRate()
</script>

<template>
  <div class="risk">
    <van-row gutter="6">
      <van-col span="6">
        <div class="risk-wrap" :class="{'active':current === 0}">
          <div class="risk__score" :style="leftStyle">
            <span>Z值{{ score }}分</span>
          </div>
          <div class="risk__item">
            <div class="process process_color_1"></div>
          </div>
          <div class="risk__txt">1.88</div>
        </div>
      </van-col>
      <van-col span="9">
        <div class="risk-wrap" :class="{'active':current === 1}">
          <div class="risk__score" :style="leftStyle">
            <span>Z值{{ score }}分</span>
          </div>
          <div class="risk__item">
            <div class="process process_color_2"></div>
          </div>
          <div class="risk__txt">2.99</div>
        </div>
      </van-col>
      <van-col span="9">
        <div class="risk-wrap" :class="{'active':current === 2}">
          <div class="risk__score" :style="leftStyle">
            <span>Z值{{ score }}分</span>
          </div>
          <div class="risk__item">
            <div class="process process_color_3"></div>
          </div>
        </div>
      </van-col>
    </van-row>
  </div>
</template>
<style scoped lang="scss">
$mainColor: #FF626D;
$colobBg: #FFECED;
.risk-wrap {
  display: flex;
  flex-direction: column;

  &.active {
    .risk__score {
      visibility: visible;
      transition: all 0.5s;
    }

    .risk__txt {
      color: $mainColor;
    }
  }

  .risk__score {
    visibility: hidden;
    position: relative;
    left: 50%;
    transform: translate(-50%, 0);
    flex: none;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 16px;
    width: 70px;
    height: 22px;
    background: $colobBg;
    border: 1px solid $mainColor;

    span {
      text-align: center;
      color: $mainColor;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      font-size: 12px;
      line-height: 17px;
    }

    &::after {
      position: absolute;
      left: 50%;
      top: 20px;
      content: " ";
      height: 6px;
      border-left: 1px solid $mainColor;
      margin-left: -1px;
    }
  }

  .risk__item {
    margin-top: 5px;
    width: 100%;
    height: 6px;
    background: #F1F1F3;
    overflow: hidden;

    .process {
      width: 100%;
      height: 6px;
      background: $mainColor;

      &.process_color_1 {
        background: #FF767F
      }

      &.process_color_2 {
        background: #FFC063
      }

      &.process_color_3 {
        background: #7189FF
      }
    }
  }

  .risk__txt {
    flex: none;
    margin-top: 4px;
    text-align: center;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    font-size: 12px;
    line-height: 17px;
    position: relative;
    left: 50%;
    //border:1px solid red;
  }
}

</style>